<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<link rel="stylesheet" href="../../assets/css/layui.css">
		<link rel="stylesheet" href="../../assets/css/view.css"/>
		<title></title>
	</head>
	<body class="layui-view-body">
		<div class="layui-content">
			<div class="layui-row">
				<div class="layui-card">
					<div class="layui-card-header">添加快递员</div>
					<form id="form" class="layui-form layui-card-body" >
						<div class="layui-form-item">
							<label class="layui-form-label">姓名</label>
							<div class="layui-input-block">
								<input type="text" name="cname" required  lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">手机号码</label>
							<div class="layui-input-block">
								<input type="text" name="cphonenumber" required  lay-verify="required" placeholder="请输入手机号码" autocomplete="off" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">身份证号码</label>
							<div class="layui-input-block">
								<input type="text" name="cidcardnum" required  lay-verify="required" placeholder="请输入身份证号码" autocomplete="off" class="layui-input">
							</div>
						</div>
						<div class="layui-form-item">
							<label class="layui-form-label">密码</label>
							<div class="layui-input-block">
								<input type="text" name="cpassword" required  lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
							</div>
						</div>

						<div class="layui-form-item">
							<div class="layui-input-block">
								<button id="submitBtn" class="layui-btn layui-btn-blue" lay-submit lay-filter="formDemo">立即提交</button>
								<button type="reset" class="layui-btn layui-btn-primary">重置</button>
							</div>
						</div>
					</form>
				</div>
			</div>
		</div>
		<script src="../../assets/layui.all.js"></script>
		<script src="/qrcode/jquery2.1.4.js"></script>
		<script src="/layer/layer.js"></script>
		<script>
			var form = layui.form
					,layer = layui.layer;
		</script>
		<script>
			//表单验证变量
			var flag_name;
			var flag_tel;
			var flag_idcard;
			var flag_pwd;
			//表单验证方法
			function checkSubmit(){
				$("input:eq(0)").blur(function () {
					if ($(this).val() == ""){
						layer.tips("姓名不能为空!",$(this),{tipsMore:true,tips:1});
						flag_name = false;
					}else if ($(this).val().length > 20){
						layer.tips("姓名长度在20个字符以内!",$(this),{tipsMore:true,tips:1});
						flag_name = false;
					}else {
						flag_name = true;
					}
				});
				$("input:eq(1)").blur(function () {
					var reg_tel = /^1[3-9](\d){9}$/;
					if ($(this).val() == ""){
						layer.tips("手机号不能为空!",$(this),{tipsMore:true,tips:1});
						flag_tel = false;
					}else if (!reg_tel.test($(this).val())){
						layer.tips("请输入正确的11位手机号!",$(this),{tipsMore:true,tips:1});
						flag_tel = false;
					}else {
						flag_tel = true;
					}
				});
				$("input:eq(2)").blur(function () {
					var reg_id = /^[1-9]\d{5}(19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
					if ($(this).val() == ""){
						layer.tips("身份证号不能为空!",$(this),{tipsMore:true,tips:1});
						flag_idcard = false;
					}else if (!reg_id.test($(this).val())){
						layer.tips("请输入正确的身份证号!",$(this),{tipsMore:true,tips:1});
						flag_idcard = false;
					}else {
						flag_idcard = true;
					}
				});
				$("input:eq(3)").blur(function () {
					var reg_pwd = /^(?=.*\d)(?=.*[a-zA-Z])(?=.*[~!@#$%^&*])[\da-zA-Z~!@#$%^&*]{8,20}$/;
					if ($(this).val() == ""){
						layer.tips("密码不能为空!",$(this),{tipsMore:true,tips:1});
						flag_pwd = false;
					}else if (!reg_pwd.test($(this).val())){
						layer.tips("密码必须包含数字、英文字母、特殊符号且在8~20位字符之间!",$(this),{tipsMore:true,tips:1});
						flag_pwd = false;
					}else {
						flag_pwd = true;
					}
				})
			};

			//符合要求才请求服务器
			$(function () {
				checkSubmit();
				$("#form").submit(function () {
					$("input:eq(0),input:eq(1),input:eq(2),input:eq(3)").trigger("blur");
					if (flag_name && flag_tel && flag_idcard && flag_pwd){
						var windowId = layer.load();
						var cname = $("input:eq(0)").val();
						var cphonenumber = $("input:eq(1)").val();
						var cidcardnum = $("input:eq(2)").val();
						var cpassword = $("input:eq(3)").val();
						$.post("/courier/insert.do",{
							cname:cname,
							cphonenumber:cphonenumber,
							cidcardnum:cidcardnum,
							cpassword:cpassword
						},function (data) {
							layer.close(windowId);
							layer.msg(data.result);
							if (data.status == 0){
								//录入成功
								//恢复表单的默认状态
								$("input").val("");
							}
						});
					}else {
						layer.msg("快递员信息不符合要求!");
					}
					return false;
				});
			});
		</script>
	</body>
</html>
